package com.tuling.proxy;

import com.tuling.proxy.advice.BeforeMethodInterceptor;
import org.aopalliance.aop.Advice;
import org.springframework.aop.*;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.aop.support.StaticMethodMatcherPointcut;

import java.lang.reflect.Method;

public class ProxyFactoryAdvicorTest {
    public static void main(String[] args) {
        ProxyFactoryBean proxyFactoryBean  = new ProxyFactoryBean();
        UserServiceInterface userServiceInterface = new UserServiceImpl();
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setTarget(userServiceInterface);
        proxyFactory.addAdvisor(new PointcutAdvisor() {

//            @Override
//            public Pointcut getPointcut() {
//                return new StaticMethodMatcherPointcut() {
//                    @Override
//                    public boolean matches(Method method, Class<?> targetClass) {
//
//                        return  "test".equals(method.getName()) ? true:false;
//                    }
//                };
//            }
            @Override
            public Pointcut getPointcut() {
                return new Pointcut() {
                    @Override
                    public ClassFilter getClassFilter() {
                        return new ClassFilter() {
                            @Override
                            public boolean matches(Class<?> clazz) {
//                                return false;
                                return clazz.equals(UserServiceClass.class);
                            }
                        };
                    }

                    @Override
                    public MethodMatcher getMethodMatcher() {
                        return new MethodMatcher() {
                            @Override
                            public boolean matches(Method method, Class<?> targetClass) {
//                                return false;
                                return method.getName().equals("test");
                            }

                            @Override
                            public boolean isRuntime() {
                                return false;
                            }

                            @Override
                            public boolean matches(Method method, Class<?> targetClass, Object... args) {
                                return false;
                            }
                        };
                    }
                };
            }

            @Override
            public Advice getAdvice() {
                return new BeforeMethodInterceptor();
            }

            @Override
            public boolean isPerInstance() {
                return false;
            }
        });
    }
}
